package com.ruoyi.blog.controller;

import com.ruoyi.blog.core.controller.BaseController;
import com.ruoyi.blog.core.domain.AjaxResult;
import com.ruoyi.blog.core.page.TableDataInfo;
import com.ruoyi.blog.domain.BlogCategory;
import com.ruoyi.blog.domain.BlogTags;
import com.ruoyi.blog.domain.vo.BlogCategoryVo;
import com.ruoyi.blog.service.BlogCategoryService;
import com.ruoyi.blog.service.BlogTagsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.blog.utils.DateUtils.getNowDate;

/**
 * @Author: 依梦
 * @Date: 2024/10/10
 * @Description: CategoryController
 */
@RestController
@RequestMapping("/blog/category")
public class BlogCategoryController extends BaseController {

    @Resource
    private BlogCategoryService blogCategoryService;

    @Resource
    private BlogTagsService blogTagsService;


    @GetMapping("/list")
    public TableDataInfo getCategoryList(BlogCategory blogCategory) {
        startPage();
        blogCategory.setCreateBy(getUserId());
        List<BlogCategoryVo> list = blogCategoryService.getList(blogCategory);
        return getDataTable(list);
    }

    @GetMapping("/all")
    public AjaxResult getAllCategoryList() {
        BlogCategory blogCategory = new BlogCategory();
        blogCategory.setCreateBy(getUserId());
        blogCategory.setStatus(0);
        List<BlogCategoryVo> list = blogCategoryService.getList(blogCategory);
        return AjaxResult.success(list);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody BlogCategory blogCategory) {
        // 校验
        if (blogCategory.getBcName() == null) {
            return AjaxResult.error("请输入分类名称!");
        }
        // 填充其他数据
        blogCategory.setCreateBy(getUserId());
        blogCategory.setCreateTime(getNowDate());
        blogCategory.setUpdateBy(getUserId());
        blogCategory.setUpdateTime(getNowDate());
        return toAjax(blogCategoryService.save(blogCategory));
    }

    @PutMapping("/update")
    public AjaxResult updateById(@RequestBody BlogCategory blogCategory) {
        blogCategory.setUpdateBy(getUserId());
        blogCategory.setUpdateTime(getNowDate());
        return toAjax(blogCategoryService.updateById(blogCategory));
    }

    @DeleteMapping("/remove")
    public AjaxResult removeById(Long bcId) {
        return toAjax(blogCategoryService.removeById(bcId));
    }

    @GetMapping("/cateGoryAndTags")
    public AjaxResult getCateGoryAndTags() {
        List<BlogCategory> list = blogCategoryService.list();
        List<BlogTags> tags = blogTagsService.list();
        Map<String, Object> map = new HashMap<>();
        map.put("cateGory", list);
        map.put("tags", tags);
        return AjaxResult.success(map);
    }
}
